package com.yuxl.admin.common.security.config;

import com.yuxl.admin.common.security.service.CustomUserDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Configuration
@Component
@AllArgsConstructor
public class SecurityAutoConfig {

    private final CustomUserDetailsService customUserDetailsService;

    /**
     * 配置密码编码器
     * @return 配置好的PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
        // 使用BCrypt加密算法
    }

    /**
     * 配置认证管理器
     * @return 配置好的ReactiveAuthenticationManager
     */
    @Bean
    public ReactiveAuthenticationManager authenticationManager() {
        UserDetailsRepositoryReactiveAuthenticationManager authenticationManager =
                new UserDetailsRepositoryReactiveAuthenticationManager(customUserDetailsService);
        authenticationManager.setPasswordEncoder(passwordEncoder());  // 设置密码编码器
        return authenticationManager;
    }
}
